﻿Imports DevExpress.XtraGrid.Views.Grid

Public Class frmType
    Dim _mysql As New ClsSQLhelper
    Dim Strsql As String
    Dim on_load As Boolean = True
    Dim Primary_Code As String = ""

#Region "Function"
    Sub Load_Category()
        Try
            strsql = "Select Cate_ID,Cate_Name from categories order by Cate_ID "
            Dim DT_Category As New DataTable
            DT_Category = _mysql.GetMYSQLDataTable(strsql, "categories")
            With CboCategories
                .DataSource = DT_Category
                .DisplayMember = "Cate_Name"
                .ValueMember = "Cate_ID"
                .SelectedIndex = 0
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Category"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Section()
        Try

            Strsql = "Select Section_ID,Section_Name from sections where Cate_ID = " & Me.CboCategories.SelectedValue & " order by Section_ID "
            Dim DT_Section As New DataTable
            DT_Section = _mysql.GetMYSQLDataTable(strsql, "section")
            ' If DT_Section.Rows.Count = 0 Then Exit Sub
            With CboSections
                .DataSource = DT_Section
                .DisplayMember = "Section_Name"
                .ValueMember = "Section_ID"
                If DT_Section.Rows.Count > 0 Then
                    .SelectedIndex = 0
                    Me.TxtType.Text = Me.CboSections.SelectedValue
                Else
                    Me.CboSections.Text = ""
                    Me.TxtType.Text = ""
                End If
            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Section"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Sub Load_Class(Optional ByVal Cretiria As String = "")
        Try
            Strsql = "Select Type_ID,Type_Name,Type_Desc from typeitem   "

            Dim StrCode As String = ""
            Dim DT_SectionCode As New DataTable

            Dim StrSectionSQL As String = "Select section_id,ifnull(Cate_Code,'') as Cate_Code,ifnull(Section_Code,'') as Section_Code from sections where section_id =  " & Me.CboSections.SelectedValue
            DT_SectionCode = _mysql.GetMYSQLDataTable(StrSectionSQL, "typeitem")
            If DT_SectionCode.Rows(0).Item("Cate_Code").ToString = "" Then
                StrCode = DT_SectionCode.Rows(0).Item("section_id").ToString
            Else
                StrCode = DT_SectionCode.Rows(0).Item("Cate_Code").ToString & "-" & DT_SectionCode.Rows(0).Item("Section_Code").ToString
            End If

            Strsql = Strsql & " where Type_ID like '" & StrCode & "%'"

            If Cretiria <> "" Then
                Select Case Me.SearchOption.EditValue
                    Case 0
                        Strsql = Strsql & " and Type_ID = '" & Cretiria & "'"
                    Case 1
                        Strsql = Strsql & " and Type_Name =  '" & Cretiria & "'"
                End Select
            End If
            Dim DT_Section As New DataTable
            DT_Section = _mysql.GetMYSQLDataTable(Strsql, "section")
            With GridType
                .DataSource = DT_Section

            End With
            Me.GridColumn1.FieldName = "Type_ID"
            Me.GridColumn2.FieldName = "Type_Name"
            Me.GridColumn3.FieldName = "Type_Desc"

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Class"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
#End Region
    Private Sub frmType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            Load_Category()
            Load_Section()
            Load_Class()


            on_load = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Load_Class()
    End Sub

    Private Sub CboSection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If on_load = True Then Exit Sub
        Load_Class()
    End Sub

    Private Sub MnAddHistoryPlace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnAddHistoryPlace.Click

        If frmNewType.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Load_Class()
        End If
    End Sub


    Private Sub BtnSearch_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnSearch.ItemClick
        If Me.TxtSearch.EditValue = "" Then Exit Sub

        Load_Class(Me.TxtSearch.EditValue)
    End Sub




    Private Sub MnDeleteHistoryPlace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnDeleteHistoryPlace.Click
        Try

            If (MsgBox("กรุณายืนยันการลบข้อมูล รหัสครุภัณฑ์ " & Me.TxtSearch.EditValue, MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            If (MsgBox("การลบข้อมูลเมื่อยืนยันแล้วจะไม่สามารถนำกลับมาใหม่ได้ คุณยังต้องการยืนยันใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            Strsql = "Select count(*) as cc from typeitem where Type_ID = '" & Me.TxtSearch.EditValue & "'"
            If (_mysql.MySQLExecuteScalar(Strsql)) = 0 Then
                MsgBox("ไม่พบรหัสครุภัณฑ์ที่ต้องการลบ", MsgBoxStyle.Critical, "กรุณาตรวจสอบรายการลบ")
                Exit Sub
            End If


            Strsql = "Delete from typeitem where  Type_ID = '" & Me.TxtSearch.EditValue & "'"

            Select Case (_mysql.MySQLExecuteScalar(Strsql))
                Case -1


                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "MnDeleteHistoryPlace_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
                    Exit Sub
                Case Else
                    MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบข้อมูล")


            End Select

            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (10,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Me.TxtSearch.EditValue & "') "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1

                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "MnDeleteHistoryPlace_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select

            Me.TxtSearch.EditValue = ""
            Load_Class()
        Catch ex As Exception
            ' MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub MnEditHistoryPlace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnEditHistoryPlace.Click
        Try
            Strsql = "Select count(*) as cc from typeitem where Type_ID = '" & Me.TxtSearch.EditValue & "'"
            If (_mysql.MySQLExecuteScalar(Strsql)) <> 0 Then
                Strsql = "Select * from typeitem where Type_ID = '" & Me.TxtSearch.EditValue & "'"
                Dim DT_Type As New DataTable
                DT_Type = _mysql.GetMYSQLDataTable(Strsql, "typeitem")

                With frmEditType
                    .CboCategory.SelectedValue = CboCategories.SelectedValue
                    .CboSection.SelectedValue = CboSections.SelectedValue
                    .TxtType.Text = Mid(Me.TxtSearch.EditValue, 1, 4)
                    .Txtkind.Text = Mid(Me.TxtSearch.EditValue, 6, 3)
                    .TxtAttribute.Text = Mid(Me.TxtSearch.EditValue, 10, 4)
                    .TxtTypeName.Text = DT_Type.Rows(0).Item("Type_Name").ToString
                    .MemoDesc.Text = DT_Type.Rows(0).Item("Type_Desc").ToString



                    .ShowDialog()
                End With
            End If

        Catch ex As Exception

            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "MnEditHistoryPlace_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub


    Private Sub CboCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategories.SelectedIndexChanged
        If on_load = True Then Exit Sub
        Load_Section()
    End Sub

    Private Sub CboSections_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboSections.SelectedIndexChanged
        If on_load = True Then Exit Sub
        Load_Class()
    End Sub

    Private Sub SearchOption_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles SearchOption.ItemClick

    End Sub

    Private Sub GridType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridType.Click

    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try

            Dim Gv As GridView = sender
            Dim CodeName As String

            'Dim TypeCODE As String = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Item_Type")).ToString()
            If Me.SearchOption.EditValue = 0 Then
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Type_ID")).ToString()
            Else
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Type_Name")).ToString()
            End If
            Primary_Code = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Type_ID")).ToString()

            Me.TxtSearch.EditValue = CodeName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "GridView1_CustomRowCellEditForEditing"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
End Class